var express = require('express');
var router = express.Router();
var { uploadFile } = require("../utils/upload.js")
const { get, add, remove, update } = require("../service/goodsService")
// router.post('/upload', async function (req, res, next) {
//     const { data, success } = await uploadFile(req, {
//         "fileType": "students",
//         "path": "./public/img/"
//     })
//     success ? res.send({ url: data.url }) : res.send({})
// });


// router.delete('/img', async function (req, res, next) {//数据删除
//     const { _id, url } = req.body
//     if (_id) {
//         update(_id, { $pull: { imgs: url } });
//     }
//     const data = await deleteImg(url)
//     res.send(data)
// });

router.get('/',async function (req, res, next) {
    // const parameter = req.query;
    const data = await get();
    res.send(data);
});

router.post('/',async function (req, res, next) {
    const parameter = req.body;
    const data = await add(parameter)
    res.send(data)
});

router.delete('/:_id',async function (req, res, next) {
    const { _id } = req.params;
    const data = await remove(_id)
    res.send(data)
});

router.patch('/:_id',async function (req, res, next) {
    const { _id } = req.params;
    const parameter = req.body;
    const data = update(_id, parameter);
    res.send(data)
});

router.post('/upload', async function (req, res, next) {
    const { data, success } = await uploadFile(req, {
        "fileType": "goods",
        "path": "./public/img/"
    })
    success ? res.send({ url: data.url }) : res.send({})
});

module.exports = router;
